package com.kxf.utils;

import android.text.TextUtils;
import android.util.Log;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.InputStreamReader;

public class CommandUtil {
    private CommandUtil() {

    }

    /**
     * 应用程序运行命令获取 Root权限，设备必须已破解(获得ROOT权限)
     */
    public static boolean RootCommand(String command, String suPath) {
        Process process = null;
        DataOutputStream os = null;
        if (TextUtils.isEmpty(suPath)){
            suPath = "su";
        }
        try {
            process = Runtime.getRuntime().exec(suPath);
            os = new DataOutputStream(process.getOutputStream());
            os.writeBytes(command + "\n");
            os.writeBytes("exit\n");
            os.flush();
            process.waitFor();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            try {
                if (os != null) {
                    os.close();
                }
                process.destroy();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        Log.d("CommandUtil", "Root SUC ");
        return true;
    }

    public static boolean checkSuFile() {
        Process process = null;
        BufferedReader reader = null;
        BufferedReader readerError = null;
        try {
            //   /system/xbin/which 或者  /system/bin/which
            process = Runtime.getRuntime().exec(new String[]{"which", "su"});
            reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line = null;
            while ((line = reader.readLine()) != null){
                System.out.println("checkSuFile==" + line);
            }
            readerError = new BufferedReader(new InputStreamReader(process.getErrorStream()));
            while ((line = readerError.readLine()) != null){
                System.out.println("readerError==" + line);
            }
            int exitVal = process.waitFor();
            System.out.println("exitVal==" + exitVal);
            return true;
        } catch (Throwable t) {
            t.printStackTrace();
            return false;
        } finally {
            if (process != null) process.destroy();
            if (reader != null) {
                try {
                    reader.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (readerError != null) {
                try {
                    readerError.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static File checkRootFile() {
        File file = null;
        String[] paths = {"/sbin/su", "/system/bin/su", "/system/xbin/su", "/data/local/xbin/su", "/data/local/bin/su", "/system/sd/xbin/su",
                "/system/bin/failsafe/su", "/data/local/su"};
        for (String path : paths) {
            file = new File(path);
            if (file.exists()) return file;
        }
        return null;
    }
}
